package zuo.biao.library.ui;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.DashPathEffect;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.PathEffect;
import android.graphics.PorterDuff;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;

import java.util.ArrayList;
import java.util.List;

import zuo.biao.library.R;
import zuo.biao.library.base.BaseActivity;
import zuo.biao.library.base.BaseAdapter;
import zuo.biao.library.model.Entry;

public class SelectLineModeActivity extends BaseActivity implements AlertDialog.OnDialogButtonClickListener {
    private final String TAG = "SelectLineModeActivity";

    //启动方法<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
    static final String INTENT_DATA_COLORINDEX = "INTENT_DATA_COLORINDEX";
    static final String INTENT_DATA_LINEINDEX = "INTENT_DATA_LINEINDEX";

    /**
     * 启动这个Activity的Intent
     *
     * @param context
     * @return
     */

    public static Intent createIntent(Context context, int colorIndex, int lineIndex) {
        return new Intent(context, SelectLineModeActivity.class)
                .putExtra(INTENT_DATA_COLORINDEX, colorIndex)
                .putExtra(INTENT_DATA_LINEINDEX, lineIndex);
    }

    //启动方法>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
    int _index = -1;
    int _colorIndex = 0;
    ;//不改变

    @Override
    public Activity getActivity() {
        return this;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.selectlinemode_activity);

        intent = getIntent();
        _index = intent.getIntExtra(INTENT_DATA_LINEINDEX, _index);
        _colorIndex = intent.getIntExtra(INTENT_DATA_COLORINDEX, _colorIndex);

        if (_colorIndex < 0 || _colorIndex >= COLORS.length) {
            _colorIndex = 0;
        }

        //功能归类分区方法，必须调用<<<<<<<<<<
        initView();
        initData();
        initEvent();
        //功能归类分区方法，必须调用>>>>>>>>>>
    }


    //UI显示区(操作UI，但不存在数据获取或处理代码，也不存在事件监听代码)<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
    //示例代码<<<<<<<<
    private ListView lvLine;

    //示例代码>>>>>>>>
    @Override
    public void initView() {//必须在onCreate方法内调用
        autoSetTitle();//自动设置标题为上个Activity传入的INTENT_TITLE

        //示例代码<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
        lvLine = (ListView) findViewById(R.id.lvLine);
        //示例代码>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
    }

    //示例代码<<<<<<<<
    private DemoAdapter adapter;
    //示例代码>>>>>>>>

    /**
     * 示例方法 ：显示列表内容
     *
     * @param list
     */
    private void setList(List<Entry<Integer, String>> list) {
        if (adapter == null) {
            adapter = new DemoAdapter(context);
            lvLine.setAdapter(adapter);
        }
        adapter.setSelected(_index);
        adapter.refresh(list);


        if (_index > 0) {
            if (android.os.Build.VERSION.SDK_INT >= 8) {
                lvLine.smoothScrollToPosition(_index);
            } else {
                lvLine.setSelection(_index);
            }
        }

    }
    //UI显示区(操作UI，但不存在数据获取或处理代码，也不存在事件监听代码)>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>


    //Data数据区(存在数据获取或处理代码，但不存在事件监听代码)<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
    //示例代码<<<<<<<<
    private List<Entry<Integer, String>> datatypelist;

    //示例代码>>>>>>>>>
    @Override
    public void initData() {//必须在onCreate方法内调用
        //示例代码<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
        showProgressDialog(R.string.loading);
        runThread(TAG + "initData", new Runnable() {
            @Override
            public void run() {
                datatypelist = getList();
                runUiThread(new Runnable() {
                    @Override
                    public void run() {
                        dismissProgressDialog();
                        setList(datatypelist);
                    }
                });
            }
        });
        //示例代码>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
    }

    private static final String[] COLORS = {"#AA181818", "#AADD3833", "#AA47B1F9", "#AA868755", "#AAF4AA2B"};

    protected List<Entry<Integer, String>> getList() {
        datatypelist = new ArrayList<Entry<Integer, String>>();
        for (int i = 0; i < 6; i++) {
            datatypelist.add(new Entry<Integer, String>(i, ""));
        }
        return datatypelist;
    }

    //Data数据区(存在数据获取或处理代码，但不存在事件监听代码)>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>


    //Event事件区(只要存在事件监听代码就是)<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
    @Override
    public void initEvent() {//必须在onCreate方法内调用
        //示例代码<<<<<<<<<<<<<<<<<<<
        lvLine.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                selectType(position);
            }
        });

        //分页滚动示例代码<<<<<<<<<<<<<<<<<<<
        new PageScroller(lvLine).init();
        //分页滚动示例代码>>>>>>>>>>>>>>>>>>>

        //示例代码>>>>>>>>>>>>>>>>>>>
    }

    public static final String RESULT_VALUE = "RESULT_VALUE";

    private void selectType(int position) {
        _index = position;
        finish();
    }

    //系统自带监听方法<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
    //@user 新增方法

    @Override
    public void finish() {
        setResult(_index);
        super.finish();
    }


    @Override
    public void onForwardClick(View v) {

    }

    @Override
    public void onDialogButtonClick(int requestCode, boolean isPositive) {
        if (isPositive == false) {
            return;
        }

    }

    //系统自带监听方法>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
    //Event事件区(只要存在事件监听代码就是)>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>


    //Adapter
    public class DemoAdapter extends BaseAdapter<Entry<Integer, String>> {
        public DemoAdapter(Activity context) {
            super(context);
        }

        private int mSelected = -1;

        public void setSelected(int selected) {
            mSelected = selected;
            notifyDataSetChanged();
        }

        @Override
        public View getView(final int position, View convertView, ViewGroup parent) {
            ViewHolder holder = convertView == null ? null : (ViewHolder) convertView.getTag();
            if (holder == null) {
                convertView = inflater.inflate(R.layout.list_viewitem, parent, false);
                holder = new ViewHolder();
                holder.iv_item = (View) convertView.findViewById(R.id.iv_item);
                convertView.setTag(holder);
            }

            int width = 200;
            int height = 26;
            Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888); //设置位图的宽高,bitmap为透明
            Canvas canvas = new Canvas(bitmap);
            canvas.drawColor(Color.TRANSPARENT, PorterDuff.Mode.CLEAR);//设置为透明，画布也是透明

            int index = position;
            if (index < 0 || index >= 6) {
                index = 0;
            }

            PathEffect pathEffect = null;
            switch (index) {
                default:
                case 0://实线
                    pathEffect = new DashPathEffect(new float[]{5, 0}, 0);
                    break;
                case 1://虚线1
                    pathEffect = new DashPathEffect(new float[]{5, 5, 5, 5}, 1);
                    break;
                case 2://虚线2
                    pathEffect = new DashPathEffect(new float[]{10, 10}, 1);
                    break;
                case 3://虚线2
                    pathEffect = new DashPathEffect(new float[]{20, 10, 5, 10}, 0);
                    break;
                case 4://虚线2
                    pathEffect = new DashPathEffect(new float[]{20, 10}, 0);
                    break;
                case 5://虚线2
                    pathEffect = new DashPathEffect(new float[]{12, 8, 6, 4}, 0);
                    break;
            }

            Paint paint = new Paint();
            paint.setStyle(Paint.Style.STROKE);
            paint.setStrokeWidth(4);
            paint.setColor(Color.parseColor(COLORS[_colorIndex]));
            paint.setAntiAlias(true);
            paint.setPathEffect(pathEffect);

            Path path = new Path();
            path.moveTo(20, height / 2);
            path.lineTo(width-20, height / 2);
            canvas.drawPath(path, paint);

            Drawable drawable = new BitmapDrawable(bitmap);
            holder.iv_item.setBackgroundDrawable(drawable);


            return super.getView(position, convertView, parent);
        }

        class ViewHolder {
            public View iv_item;
        }

        private final long serialVersionUID = 3L;

        @Override
        public long getItemId(int position) {
//            return getItem(position).getId();
            return serialVersionUID + position;
        }

    }


}